import {cut_down} from "../../../mixins/count_down";
const app = getApp();
Component({
    properties:{
        goodsList:{
            type:Object,
            value:null,
            observer:"changeGoods"
        }
    },
    data:{
        goods:null,
        serverTime:new Date().getTime(),
        Height:0,
        curentDot:0,
        isautoplay:false,
        specially:"#00CC66",//主题色
        costar:'#FF0066',//配色
        // theme
    },
    behaviors:[cut_down],
    attached(){
      this.setData({
        serverTime: new Date().getTime(),
        specially:app.theme.specially,
        costar:app.theme.costar
      })
    },
    detached (){
		  clearInterval(this.data.interval);
    },

    methods:{
      dotChange(e){
        // console.log(e.detail.current)
        this.setData({
          curentDot: e.detail.current
        })
      },
      getPoster(){
        this.triggerEvent('getPoster')
        // this.selectComponent("#share").ProductShare(this.data.goods);
      },
      playVideo(){
        this.data.videoContext = wx.createVideoContext('myVideo');
        
        this.setData({
          isautoplay:true,
          ['goods.play']:true
        })
        this.data.videoContext.play();
      },
      stopVideo(){
        this.setData({
          ['goods.play']: false
        })
        this.data.videoContext.play();
      },
        changeGoods(newValue){
          // let config= app.functions.getLocalStorage("config");
            if(Object.keys(newValue).length !== 0) {
                newValue.startTime = this.handlestartTime(newValue);
                newValue.imageUrls = this.handleimageUrls(newValue);
                newValue.endTime = this.handleendTime(newValue);
                newValue.leftTime = this.handleleftTime(newValue);
                newValue.isSoldOut = this.handleisSoldOut(newValue);
                newValue.play = false; 
                newValue.statusText= this.statusText(newValue);
                // newValue.statusStyle= this.statusStyle(newValue);
               
                // console.log(newValue.faHuoTime)
                // newValue.pickupTime = app.functions.dateTimeFormatMd(app.functions.pickupTime(newValue));
                if (newValue.statusText == '即将结束') {this.startTimer(newValue.leftTime)}
                this.setData({
                    goods:this.properties.goodsList
                })
            }
               
        },
        async imgHeight(e){
            let systemInfo = await wx.getSystemInfoSync();
            let windowWidth = systemInfo.windowWidth;
            let imgh=e.detail.height;//图片高度
            let imgw=e.detail.width;//图片宽度
            let swiperH=windowWidth*imgh/imgw*2 + "rpx"//等比设置swiper的高度。 即 屏幕宽度 / swiper高度 = 图片宽度 / 图片高度  ==》swiper高度 = 屏幕宽度 * 图片高度 / 图片宽度
            this.setData({
                Height:swiperH
            })
          },
        statusText(element){
            if (element.endTime === 0) {
                 return "团购已结束";
               }
   
               if (element.startTime > 0) {
                 return "即将开团";
               }
   
               if (element.endTime > 0) {
                 if (element.isSoldOut) {
                   return "已售罄";
                 }
   
                 return "即将结束";
               }
          },
        //   statusStyle(element){
        //    if (!element.isSoldOut && element.leftTime > 0) {
        //      return  "margin-top:14rpx;margin-left:-60rpx;color:gray;"
        //  } else {
        //      return "line-height:120rpx;color:gray;"
        //  }
        //   },
        handleimageUrls(element) {
            let images = [
                element.thumbSquare.slice(0, 4) === "http"
                    ? element.thumbSquare
                    : app.globalData.IMAGE_PREFIX + element.thumbSquare
            ];
            let thumbUrl = element.thumbUrl;

            if (!thumbUrl) {
                return images;
            }

            if (thumbUrl.slice(0, 1) === "a") {
                let tmp = thumbUrl.slice(4);
                let content = tmp.split(";");

                content.forEach(element => {
                    if (element.slice(0, 1) === "s") {
                        let urls = element.split(":");
                        let url = urls[2];

                        for (let i = 3; i < urls.length; ++i) {
                            url += ":" + urls[i];
                        }

                        url = url.substr(1, url.length - 2); //去除两端引号
                        url = url.slice(0, 8) === "https://" ? url : app.globalData.IMAGE_PREFIX + url;
                        images.push(url);
                    }
                });
            } else {
                let url = thumbUrl.split(",");
                images.push(...url);
            }
            return images;
        },
        handlestartTime(element){
            const time = Date.parse(element.timestart.replace(/-/g, '/')) - this.data.serverTime;
            return (time > 0) ? time : 0;
        },
        handleendTime(element) {
            let timeend = element.timeend;
            const time =
              Date.parse(timeend.replace(/-/g, "/")) - this.data.serverTime;
            return time > 0 ? time : 0;
          },
          handleleftTime(element) {
            if (this.startTime > 0) {
              return this.startTime;
            } else if (element.endTime > 0) {
              return element.endTime;
            } else {
              return 0;
            }
          },
          handleisSoldOut(element) {
            // if (!isNaN(parseInt(element.remainderStock))) {
            if(element.jgProductId) {
                return (element.remainderStock == 0) || (element.total <= element.salesreal);
            }
            return element.total <= element.salesreal;
        }
    }
})